package com.zany.net;

import com.google.gson.*;
import com.zany.net.dto.Header;
import com.zany.net.dto.InterfaceSerializer;
import com.zany.net.dto.request.QueryMoneyReq;
import com.zany.net.dto.request.RequestBody;
import com.zany.net.dto.request.RequestEntity;
import com.zany.net.service.NetService;
import com.zany.net.service.QueryMoneyServiceImpl;
import org.apache.commons.beanutils.BeanUtils;

import java.lang.reflect.InvocationTargetException;
import java.util.Iterator;
import java.util.Map;
import java.util.Set;

public class Test {

    public static void main(String[] args) {
//        WechatServer server = new WechatServer();
//
//        Observer userZhang = new User("ZhangSan");
////        Observer userLi = new User("LiSi");
////        Observer userWang = new User("WangWu");
//
//        server.registerObserver(userZhang);
////        server.registerObserver(userLi);
////        server.registerObserver(userWang);
//        server.setInfomation("PHP是世界上最好用的语言！");
////
////        System.out.println("----------------------------------------------");
////        server.removeObserver(userZhang);
//        server.setInfomation("JAVA是世界上最好用的语言！");

        QueryMoneyReq request = new QueryMoneyReq();
        request.setOrgCode("1111");
        request.setQueryParam("123566");
        request.setQueryType("1");

        Header header = new Header();
        header.setChannal("BANK");
        header.setMsgcd("100");
        header.setSeqnb("12345678");
        header.setSndappcd("12345678");
        header.setSnddt("20181207111900");

        RequestEntity requestEntity = new RequestEntity();
        requestEntity.setBody(request);
        requestEntity.setHeader(header);

        Gson gson = new Gson();
        String jsonStr = gson.toJson(requestEntity, RequestEntity.class);
        System.out.println(jsonStr);
//        RequestEntity<QueryMoney> re = new RequestEntity<QueryMoney>();
//        re.setBody(new QueryMoney());


        InterfaceSerializer<QueryMoneyReq> interfaceSerializer = InterfaceSerializer.interfaceSerializer(QueryMoneyReq.class);
//
        Gson gson1 = new GsonBuilder()
                .registerTypeAdapter(RequestBody.class, interfaceSerializer)
                .create();
        RequestEntity entity = gson1.fromJson(jsonStr,  RequestEntity.class);
        System.out.println(entity);
        QueryMoneyReq body = (QueryMoneyReq) entity.getBody();
        System.out.println(body.getOrgCode());
        System.out.println(body.getQueryParam());
        System.out.println(body.getQueryType());
//
//
//        NetService netService = new QueryMoneyServiceImpl();
//        netService.handler(jsonStr);


//        Gson gson2 = new GsonBuilder().create();
//        RequestEntity requestEntity1 = gson2.fromJson(jsonStr, RequestEntity.class);
//        System.out.println(requestEntity1);

//
        JsonParser parser = new JsonParser();
        JsonObject jsonObj = parser.parse(jsonStr).getAsJsonObject();
        JsonElement element = jsonObj.get("body");
        JsonElement header1 = jsonObj.get("header");
        JsonObject asJsonObject = header1.getAsJsonObject();
        String msgcd = asJsonObject.get("msgcd").getAsString();
        System.out.println(msgcd.toString().equals("100"));
//        System.out.println(element.toString());
//        Gson gson2= new Gson();
//        QueryMoneyReq queryMoneyReq = gson2.fromJson(element.toString(), QueryMoneyReq.class);
//        System.out.println(queryMoneyReq);

    }


}

